<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="searchToggle">
        <label class="el-form-item-label">姓名</label>
        <el-input v-model="query.name" clearable placeholder="姓名" style="width: 185px;" class="filter-item"
          @keyup.enter.native="toQuery" />
        <label class="el-form-item-label">省份</label>
        <el-input v-model="query.province" clearable placeholder="省份" style="width: 185px;" class="filter-item"
          @keyup.enter.native="toQuery" />
        <label class="el-form-item-label">地址</label>
        <el-input v-model="query.address" clearable placeholder="地址" style="width: 185px;" class="filter-item"
          @keyup.enter.native="toQuery" />
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      </div>
      <div class="crud-opts">
        <span class="crud-opts-left">
          <el-button
            type="primary"
            class="filter-item"
            size="mini"
            icon="el-icon-plus"
            @click="toAdd">
            新增
          </el-button>
          <el-button
            type="success"
            class="filter-item"
            size="mini"
            icon="el-icon-edit"
            @click="toEdit()"
            :disabled="multipleSelection == null || (multipleSelection.length !== 1)">
            修改
          </el-button>
          <el-button
            type="danger"
            class="filter-item"
            size="mini"
            icon="el-icon-delete"
            @click="toDelete()"
            :disabled="multipleSelection == null || (multipleSelection.length === 0)">
            删除
          </el-button>
        </span>
        <el-button-group class="crud-opts-right">
          <el-button
            size="mini"
            plain
            type="info"
            icon="el-icon-search"
            @click="searchToggle = !searchToggle"
          />
          <el-button
            size="mini"
            icon="el-icon-refresh"
            @click="toQuery()"
          />
        </el-button-group>
      </div>
      <!--表单渲染-->
      <el-dialog append-to-body :close-on-click-modal="false" :title="formTitle" :visible.sync="fromVisible"
        width="500px">
        <el-form ref="form" :inline="true" :model="form" size="small" label-width="60px">
          <el-form-item label="日期" prop="date">
            <el-date-picker v-model="form.date" type="date" placeholder="选择日期" />
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="省份" prop="province">
            <el-input v-model="form.province" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="市区" prop="city">
            <el-input v-model="form.city" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="邮编" prop="zip">
            <el-input v-model="form.zip" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="fromVisible = false">取消</el-button>
          <el-button type="primary" @click="doSubmit">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->

      <el-table
        ref="table"
        :data="tableData"
        border size="small"
        style="width: 100%;"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column fixed prop="date" label="日期" align="center">
        </el-table-column>
        <el-table-column prop="name" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="province" label="省份" align="center">
        </el-table-column>
        <el-table-column prop="city" label="市区" align="center">
        </el-table-column>
        <el-table-column prop="address" label="地址" align="center">
        </el-table-column>
        <el-table-column prop="zip" label="邮编" align="center">
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="toEdit(scope.row)"
              style="margin-right: 10px;" />
            <el-popover v-model="scope.row.pop" placement="top" width="180" trigger="manual">
              <p>{{ '确认删除吗？' }}</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope.row.pop = false">取消</el-button>
                <el-button type="primary" size="mini" @click="doDelete(scope.row)">确定</el-button>
              </div>
              <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"
                @click="toDelete(scope.row)" />
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px; width: 50vw"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[5, 10, 25, 30]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import test1 from '@/api/test1'
  const defaultForm = {
    id: null,
    date: null,
    name: null,
    province: null,
    city: null,
    address: null,
    zip: null
  }
  export default {
    created() {
      this.toQuery()
    },
    data() {
      return {
        tableData: [{
          id: 1,
          date: '2016-05-02',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }, {
          id: 2,
          date: '2016-05-04',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1517 弄',
          zip: 200333
        }],
        pop: false,
        query: {
          name: '',
          uuid: '',
          area: '',
          location: ''
        },
        formTitle: '',
        fromVisible: false,
        form: {},
        multipleSelection: [],
        paging: {
          size: 5,
          page: 0,
          sort: "id,desc"
        },
        pagination: {
          total: 0,
          currentPage: 1,
          pageSize: 5
        },
        loading: false,
        searchToggle: true
      }
    },
    methods: {
      /** 增删改查按钮触发 */
      toQuery() {
        // 查询数据
        // test1.get(this.query).then(res => {})
        // this.accountPage.total = res.data.totalElements
        this.pagination.total = 2
      },
      toAdd() {
        // 开始添加数据前
        this.formTitle = "添加数据"
        this.form = JSON.parse(JSON.stringify(defaultForm))
        this.fromVisible = true
      },
      toEdit(row) {
        // 开始编辑数据前
        this.formTitle = "编辑数据"
        if(row) {
          this.form = JSON.parse(JSON.stringify(row))
        }else {
          this.form = JSON.parse(JSON.stringify(this.multipleSelection[0]))
        }
        this.fromVisible = true
      },
      toDelete(row) {
        // 删除数据前
        if(row) {
          row.pop = true
        }else {
          if (this.multipleSelection != null) {
            const ids = this.multipleSelection.map(item => {
              return item.id
            })
            console.log("删除ids", ids);
          }
        }
      },
      doSubmit() {
        // 添加数据
        console.log('提交', this.form);
        this.fromVisible = false
      },
      doDelete(row) {
        // 删除数据
        row.pop = false
        console.log('删除', row);
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleSizeChange(val) {
        this.paging.size = val
        this.pagination.pageSize = val
        this.toQuery()
      },
      handleCurrentChange(val) {
        this.paging.page = val - 1
        this.pagination.currentPage = val
        this.toQuery()
      },
      /**-------*/

      /** 自定义方法 */

      /**-------*/
    },
  }
</script>

<style>
  .crud-opts {
    padding: 4px 0;
    display: -webkit-flex;
    display: flex;
    align-items: center;
  }

  .crud-opts .crud-opts-right {
    margin-left: auto;
  }

  .crud-opts .crud-opts-right span {
    float: left;
  }
</style>
